/*	
 *				
 *					ISNW ---- It is a war.It is not warcraft!
 *
 *
 *					Author:					zhoukeli(haxixi_keli)
 *					Filename:				str_to_num.h
 *					CreateTime:				2008/12/14
 *					ModifyTime:				2008/12/14
 */

#ifndef __ISNW_STR_TO_NUM_H__
#define __ISNW_STR_TO_NUM_H__

#include "isnwTypes.h"

namespace ISNW
{
namespace core
{
	const static f32 fast_to_float[] = {
		0.1,
		0.01,
		0.001,
		0.0001,
		0.00001,
		0.000001,
		0.0000001,
		0.00000001,
		0.000000001,
		0.0000000001,
		0.00000000001,
		0.000000000001,
		0.0000000000001,
		0.00000000000001,
		0.000000000000001,
		0.0000000000000001
	};

	inline s32 strtoi(const c8* pstr,s32& out)
	{
		s32 ret = 0;
		bool sign = false;

		s32 i = 0;
		if(pstr[0] == '-')
		{
			sign = true;
			++i;
		}
		while(pstr[i] >= '0' && pstr[i] <= '9')
		{
			s32 c = pstr[i] - '0';
			ret = ret * 10 + c;
			++i;
		}

		out = ret;
		if(sign) out *= -1;

		return i;
	}

	inline s32 strtof(const c8* pstr,f32& out)
	{
		f32 ret = 0;
		s32 integer_part = 0;
		s32 decimal_part = 0;
		s32 i = 0;
		s32 j = 0;
		bool sign = false;
		if(pstr[0] == '-') sign = true;
		i = strtoi(pstr,integer_part);
		if(sign) integer_part *= -1;

		if(pstr[i] == '.')
		{
			i++;
			j = strtoi(pstr+i,decimal_part);
			i += j;
		}

		ret = (f32)(integer_part) + (f32)(decimal_part)*fast_to_float[j-1];

		out = ret;
		if(sign) out *= -1;

		return i;
	}
} // end namespace core
} // end namespace ISNW
#endif